package com.bdqn.thymeleaf.controller;

import com.bdqn.thymeleaf.pojo.SysUser;
import com.bdqn.thymeleaf.service.ISysUserService;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
public class ExampleController {

    @Resource
    private ISysUserService sysUserService;

    @GetMapping(value = "/hello/{id}")
    public String getUser(@PathVariable("id") Long usrId, Model model) {
        SysUser sysUser = sysUserService.getUser(usrId);
        model.addAttribute("user", sysUser);
        return "demo/hello";
    }

    //    @GetMapping(value = "/string")
//    public String getString(Model model, HttpSession session){
//        model.addAttribute("userName","曹家康");
//        session.setAttribute("userName","曹家康");
//        return "demo/string";
//    }
    @GetMapping(value = "/string")
    public String getString(Model model,HttpServletRequest request) {
        model.addAttribute("userName", "曹家康");
        request.setAttribute("test","request");
        request.getSession().setAttribute("test","session");
        model.addAttribute("date",new Date());
        return "demo/string";
    }

    @GetMapping(value = "/inline")
    public String inline(Model model) {
        model.addAttribute("userName", "曹家康");
        return "demo/inline";
    }

    @GetMapping(value = "/if")
    public String ifunless(Model model){//演示条件判断 if/unless
        model.addAttribute("flag","yes");
        return "demo/if";
    }

    @GetMapping(value = "/list")
    public String list(Model model,HttpSession session){
        List<SysUser> list = sysUserService.findAllUsers();
        model.addAttribute("sysUser",list);
//        session.setAttribute("sysUser",list);
        if(list !=null){
            for (SysUser sysUser : list){
                System.out.println(sysUser.toString());
            }
        }
        return "demo/list";
    }

}

